(function(namespace) {

	var cloneTable =null;
	/**
	 * 1.arguments【0】赋值 会影响形参比如 functionmm(a,b,c){ arguments[0] =1 ; 那么a就为1了； }
	 */
	var mainDom_xcalendar = "mainDom_xcalendar";
	var headDom_xcalendar = "headDom_xcalendar";
	var bodyDom_xcalendar = "bodyDom_xcalendar"
	var bodyTable_xcalendar = "bodyTable_xcalendar";

	var cell_xcalendar = "cell_xcalendar";

	var today_xcalendar = "today_xcalendar";
	var notCurMonth_xcalendar = "notCurMonth_xcalendar";
	var selected_xcalendar = "selected_xcalendar";
	
	var mouse_over_xcalendar = "mouse_over_xcalendar";
	
	function XCalendar(options) {
		var options = this.options = options || {};
		this.wnd = options["wnd"] || window;
		this.doc = options["doc"] || this.wnd.document;
		this.parent = $(options["parent"] || this.doc.body);
		this.date = options["date"] || new Date();
		this.selected = null;
		var current = new Date();
		this.today = {
				day : current.getDate(),
				month : current.getMonth()+1,
				year : current.getFullYear()
		};
		this.lastMonths = [];
		this.currentMonths =[];
		this.nextMonths = [];
		_initUI.call(this);
	}

	function _initUI() {
		this.mainDom = $("<div>").appendTo(this.parent).addClass(
				mainDom_xcalendar);
		this.headDom = $("<div>").appendTo(this.mainDom).addClass(headDom_xcalendar);
		this.bodyDom = $("<div>").appendTo(this.mainDom).addClass(bodyDom_xcalendar);
		this.bodyTable = $("<table>").appendTo(this.bodyDom).addClass(
				bodyTable_xcalendar);
		this.weekDom = $(
				"<tr> <th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>")
				.appendTo(this.bodyTable);
		for ( var i = 0; i < 6; i++) {
			var tr = $("<tr>").appendTo(this.bodyTable);
			for ( var j = 0; j < 7; j++) {
				this[i + "" + j + "td"] = $("<td>").text("&nbsp;").addClass(
						cell_xcalendar).appendTo(tr);
				this[i + "" + j + "td"].bind({
					"click.td" :evt_click.bind(this,this[i + "" + j + "td"]),
					"mouseover.td":evt_on_mouse_over.bind(this,this[i + "" + j + "td"]),
					"mouseout.td" :evt_on_mouse_out.bind(this,this[i + "" + j + "td"]) 
				});
				this[i + "" + j + "td"].owner = this;
			}
		}

		initDate.call(this, new Date());
	}

	/**
	 * 初始化时间
	 * month 是实际月份 8就是8月
	 */
	function initDate(year, month, day) {
		this.showDate = {
				year:year,
				month : month,
		};
		this.lastMonths = [];
		this.currentMonths =[];
		this.nextMonths = [];
		if (arguments.length == 1 && arguments[0] instanceof Date) {
			var date = arguments[0];
			day = date.getDate();
			month = date.getMonth() + 1;
			year = date.getFullYear();
		}
		var firstday = new Date(year, month - 1);
		var lastMonth = month - 1;
		var lastYear = year;
		var nextMonth = month + 1;
		var nextYear = year;
		var firstweek = firstday.getDay();
		if (month == 1) {
			lastMonth = 12;
			lastYear = year - 1;
		}
		if(month == 12){
			nextMonth = 1;
			nextYear = year +1;
		}
		var lastMaxday = getMaxDays(lastYear, lastMonth);
		var days = [];
		for ( var i = 0; i < firstweek; i++) {
			days.unshift(lastMaxday);
			lastMaxday--;
		}
		var lastMonthLength = days.length;
		var curMaxday = getMaxDays(year, month);
		for ( var i = 1; i <= curMaxday; i++) {
			days.push(i);
		}
		var nextMonthLength = days.length;
		var m = 1;
		for ( var k = days.length; k < 42; k++) {
			days[k] = m++;
		}
		m = 0;
		for ( var i = 0; i < 6; i++) {
			for ( var j = 0; j < 7; j++) {
				var curday = days[m];
				var td = this[i + "" + j + "td"];
				td.attr("class","");
				td.addClass(cell_xcalendar);
				if (m < lastMonthLength || m >= nextMonthLength) {
				if(m < lastMonthLength) {
					td.day = {
						year : lastYear,
						month:lastMonth,
						day : curday
					}; 
					this.lastMonths.push(td);
				}
				if(m >= nextMonthLength) {
					td.day = {
							year : nextYear,
							month:nextMonth,
							day : curday
						}; 
					this.nextMonths.push(td);
				}
					td.addClass(notCurMonth_xcalendar);
				}else{
					td.day = {
							year : year,
							month:month,
							day : curday
						} ;
					this.currentMonths.push(td);
				}
				if (ObjEquals(this.today,td.day)) {
					setSelected.call(this, td);
					td.addClass(today_xcalendar).removeClass(cell_xcalendar);
				}
				td.text(curday);
				m++;
			}
		}
	}
	/**
	 * 获取每个月最大天数 原因不明。。
	 */
	function getMaxDays(year, month) {
		var date = new Date(year, month, 0);
		return date.getDate();
	}

	function setSelected(td) {
		if (this.selected) {
			this.selected.removeClass(selected_xcalendar);
			this.selected.removeClass(cell_xcalendar);
		}
		this.selected = $(td);
		this.selected.addClass(selected_xcalendar);
	}
	
	function evt_click(td,evt){
		var selectDay = td.day;
		var nextMonth = -1;
		var lastMonth = -1;
		var isLeft = true;
		if((lastMonth = this.lastMonths.indexOf(td)) != -1 || (nextMonth = this.nextMonths.indexOf(td)!= -1)){
			if(cloneTable) cloneTable.remove();
			cloneTable = this.bodyTable.clone();
			this.bodyDom.prepend(cloneTable);
			if(nextMonth !=-1){
				this.bodyTable.css("left",cloneTable.width()+"px");
			}else{
				isLeft = false;
				this.bodyTable.css("left",-cloneTable.width()+"px");
			}
			initDate.call(this,td.day.year,td.day.month,td.day.day);
			td = getTdByDay.call(this,selectDay);
			fadeLeft.call(this,isLeft);
		}
		this.headDom.text(this.showDate.year+"   "+this.showDate.month);
			setSelected.call(this,td);
			
	}
	
	/**
	 * 根据日期找出TD
	 */
	function getTdByDay(day){
		for ( var i = 0; i < 6; i++) {
			for ( var j = 0; j < 7; j++) {
				if(ObjEquals(this[i + "" + j + "td"].day,day)){
					return this[i + "" + j + "td"];
				}
			}
		}	
	}
	
	/**
	 * 转换的函数 一定要除以5除尽
	 */
	function fadeLeft(isLeft){
		var width = cloneTable.width();
		var cloneLeft = parseInt(cloneTable.css("left"));
		var left = parseInt(this.bodyTable.css("left"));
		if(isLeft){
			cloneTable.css("left",(cloneLeft-5)+"px");
			this.bodyTable.css("left",(left-5)+"px");
			
		}else{
			cloneTable.css("left",(cloneLeft+5)+"px");
			this.bodyTable.css("left",(left+5)+"px");
		}
		if(left != 0){
			setTimeout(fadeLeft.bind(this,isLeft),5);
		}
	}
	/**
	 * 每个格子鼠标移入事件
	 */
	function evt_on_mouse_over(td,evt){
		if(this.selected == td || this.today == td) return;
		td.addClass(mouse_over_xcalendar);
			
	}
	
	/**
	 * 每个格子鼠标移出事件
	 */
	function evt_on_mouse_out(td,evt){
		if(this.selected == td || this.today == td) return;
		td.removeClass(mouse_over_xcalendar);
	}
	
	/**
	 * 对象是否相等
	 */
	function ObjEquals(a,b){
		for(var i in a){
			if(a[i] != b[i]) return false;
		}
		return true;
	}

	namespace["XCalendar"] = XCalendar;
})(window)